
#include "download_manager.h"
//#include "db_manager.h"
#include "dict_manager.h"
#include "url_schema.h"
#include "pending_schema.h"
#include "keyword_schema.h"
#include "dict_schema.h"
#include "invalid_schema.h"
#include "conf.h"

int main(int argc, char* argv[])
{
	
	if(init_conf(g_conf)!=0)
	{
		return -1;
	}
	
	printf("starting...\n");

	
	init_dict_manager(g_dict_manager);
//	init_db_manager(g_db_manager);
	init_url_schema(g_url_schema);
	init_invalid_schema(g_invalid_schema);
	init_keyword_schema(g_keyword_schema);
	init_dict_schema(g_dict_schema);
	init_pending_schema(g_pending_schema); //must be the last
	
	
	curl_global_init(CURL_GLOBAL_ALL);
	init_download_manager(g_download_manager);
	
 	char input_url[URL_LEN];
	do
	{
		memset(input_url, 0, URL_LEN);
		printf("please input url, if input 'q' will quit:\n");
		scanf("%s", input_url);
		input_url[URL_LEN-1] = 0;
		if( strcmp(input_url, "q") == 0)
		{
			//quit
			break;
		}
		
		url_t* url = create_url(input_url,strlen(input_url));
		if(url == 0)
			continue;
			
		add_url_to_download_manager(url, &g_download_manager);
 	}while(1);
	
	printf("closing download.....\n");
	quit_download_manager(&g_download_manager);
	sleep(10);
	printf("closing dict_schema.....\n");
	quit_dict_manager(&g_dict_manager);
	sleep(10);
//	quit_db_manager(&g_db_manager);
	printf("closing url_schema.....\n");
	quit_url_schema(&g_url_schema);
	sleep(10);
	printf("closing pending_schema.....\n");
	quit_pending_schema(&g_pending_schema);
	
	sleep(10);
	printf("closing dict_schema.....\n");
	quit_invalid_schema(&g_invalid_schema);
	
	sleep(10);
	printf("closing keyword_schema.....\n");
	quit_keyword_schema(&g_keyword_schema);

	sleep(10);
	printf("closing dict_schema.....\n");
	quit_dict_schema(&g_dict_schema);


	curl_global_cleanup();
	
	printf("closed\n");
	getchar();
	return 0;
}
